﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model;
using System.Linq.Expressions;
using System.Data.Entity;

namespace BusinessLogic
{
    public class UserInfoLogic
    {
        public List<dynamic> getPageDate<T, TKey>(Expression<Func<T, dynamic>> select, Expression<Func<T, bool>> where, Expression<Func<T, TKey>> order, int pageIndex, int pageSize, out int Total)
               where T : class
        {
            Model1Container db = new Model1Container();
            Total = db.Set<T>().Where(where).Count();
            var list = db.Set<T>().Where(where).OrderByDescending(order).Select(select).Skip((pageIndex - 1) * pageSize).Take(pageSize);
            return list.ToList();
        }

     //   //使用方法
     //int Total_ = 0;
     //    rptCate.DataSource = getPageDate<Category, int>(c => new { c.Name, c.CreatedDate, c.CreatedBy }, c => c.Id > 0, c => c.Id, 2, 4, out Total　　　　　　　_);
     //    rptCate.DataBind();
     //    this.Label1.Text = Total_.ToString();
        public T_Sys_AdminUser GetForm(int keyValue)
        {
            Model1Container db = new Model1Container();
            return db.T_Sys_AdminUser.Find(keyValue);
        }

        public int AddInfo(T_Sys_AdminUser model)
        {
            int result = 0;
            try 
            {
                Model1Container db = new Model1Container();
                db.T_Sys_AdminUser.Add(model);
                db.SaveChanges();
                result = model.UserId;
            }catch(Exception ex)
            {
                return 0;
            }
            return result;
        }

        public int UpdateInfo(T_Sys_AdminUser model,string pwd)
        {
            int result = 0;
            try 
            {
                using (Model1Container db = new Model1Container())
                {
                    var newModel = db.T_Sys_AdminUser.Where(c => c.UserId == model.UserId).First();
                    newModel.LoginName = model.LoginName;
                    if (!newModel.Password.Equals(pwd))
                    {
                        newModel.Password = pwd;
                    }

                    newModel.RealName = model.RealName;
                    newModel.Phone = model.Phone;
                    newModel.Address = model.Address;
                    newModel.State = model.State;
                    //newModel.IsSystem=model.IsSystem
                    newModel.Sex = model.Sex;
                    result = db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                return 0;
            }

            return result;
        }

        public int DelInfo(int id)
        {
            int result = 0;
            try
            {
                using (Model1Container db = new Model1Container())
                {
                    var model = db.T_Sys_AdminUser.First(p => p.UserId == id);
                    model.IsDelete = true;
                    db.SaveChanges();
                    result = db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                return 0;
            }

            return result;
        }

        public int LockInfo(int id)
        {
            int result = 0;
            try
            {
                using (Model1Container db = new Model1Container())
                {
                    var model = db.T_Sys_AdminUser.First(p => p.UserId == id);
                    model.State = true;
                    db.SaveChanges();
                    result = db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                return 0;
            }

            return result;
        }
        public int UnLockInfo(int id)
        {
            int result = 0;
            try
            {
                using (Model1Container db = new Model1Container())
                {
                    var model = db.T_Sys_AdminUser.First(p => p.UserId == id);
                    model.State = false;
                    db.SaveChanges();
                    result = db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                return 0;
            }

            return result;
        }
    }
}
